package com.ruoyi.passenger.pay.controller;

import com.google.common.collect.Maps;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.HttpClientUtil;
import com.ruoyi.common.utils.order.WeixinUtil;
import com.ruoyi.management.statistical.driversign.service.ITDriverSignService;
import com.ruoyi.passenger.order.domain.TOrder;
import com.ruoyi.passenger.order.service.ITOrderService;
import com.ruoyi.passenger.orderevaluate.ro.AddOrderEvaluateParam;
import com.ruoyi.passenger.orderevaluate.service.OrderEvaluateService;
import com.ruoyi.passenger.pay.ro.AppidParam;
import com.ruoyi.passenger.pay.ro.PayParam;
import com.ruoyi.passenger.pay.service.IPayService;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 支付
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/pay")
public class PayController extends BaseController
{


    @Autowired
    private OrderEvaluateService orderEvaluateService;


    @Autowired
    private IPayService payService;

    @Autowired
    private ITDriverSignService tDriverSignService;


    @Autowired
    private ISysDictTypeService dictTypeService;


    @Autowired
    private ITOrderService tOrderService;

    @Value("${pay.params.curCode}")
    private String curCode;

    @Value("${pay.params.txCode}")
    private String txCode;

    @Value("${pay.params.bankURL}")
    private String bankURL;

    @Value("${pay.app.appid}")
    private String appid;

    @Value("${pay.app.secret}")
    private String secret;

    @Value("${pay.app.ckAppid}")
    private String ckAppid;

    @Value("${pay.app.ckSecret}")
    private String ckSecret;

    @ApiOperation(value = " 乘客支付", notes = "乘客支付")
    @GetMapping("/payWithAmounts")
    public AjaxResult payWithAmounts(@Valid PayParam param) {

        String type = "3";//获取建行支付页面
        AjaxResult payUrlResult = payService.getPayUrl(param,type);
        if ("success".equals(payUrlResult.get("result").toString()))
        {
            String payUrl = payUrlResult.get("payUrl").toString();
            return payService.getQrUrl(payUrl,(TOrder)payUrlResult.get("order"));
        }
        return payUrlResult;
    }

    @ApiOperation(value = "获取支付对象", notes = "乘客支付")
    @GetMapping("/getPayTarget")
    public AjaxResult getPayTarget(@Valid PayParam param) {
        String carNo = param.getCarNo();
        return AjaxResult.success(tDriverSignService.getDriverSignVO(carNo));
    }


    @ApiOperation(value = "获取支付结果", notes = "乘客支付")
    @GetMapping("/getPayResult")
    public AjaxResult getPayResult(@Valid PayParam param) {
        return payService.getPayResult(param);
    }


    @GetMapping("/add-orderevaluate")
    @ApiOperation("新增订单评价")
    public AjaxResult addOrderevaluate(@Valid AddOrderEvaluateParam param){
        return toAjax(orderEvaluateService.addOrderevaluate(param,"","h5"));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 小程序端获取openId
     *
     * @return
     */
    @GetMapping("/getOpenIdByJsCode")
    public AjaxResult getOpenIdByJsCode(@Valid AppidParam param) {
        String js_code = param.getCode();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + ckAppid + "&secret=" + ckSecret +
                "&js_code=" +
                js_code +
                "&grant_type=authorization_code";
        return AjaxResult.success(HttpClientUtil.sendGetRequest(url, null, null));
    }


    /**
     * 小程序端获取openId
     *
     * @return
     */
    @GetMapping("/getPassengerOpenId")
    public AjaxResult getPassengerOpenId(@Valid AppidParam param) {
        String js_code = param.getCode();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + ckAppid + "&secret=" + ckSecret + "&js_code=" + js_code + "&grant_type=authorization_code";
        return AjaxResult.success(HttpClientUtil.sendGetRequest(url, null, null));
    }

    /**
     * 小程序端获取openId
     *
     * @return
     */
    @RequestMapping("/getTest")
    public AjaxResult getTest(@Valid AppidParam param) {
        return AjaxResult.success("success");
    }

    /**
     * 小程序端获取config
     *
     * @return
     */
    @GetMapping("/getWxConfig")
    public AjaxResult getWxConfig(@Valid String requestUrl) {
        return AjaxResult.success(WeixinUtil.getWxConfig(appid,secret,requestUrl));
    }

    @GetMapping("/getOpenCode")
    public AjaxResult getOpenCode(@Valid String code) {
        return AjaxResult.success(WeixinUtil.getOpenCodeForAjax(appid,secret,code));
    }


    /**
     * 小程序端获取openId
     *
     * @return
     */
    @GetMapping("/getOpenId")
    public AjaxResult getOpenId(@Valid String requestUrl) {
        return AjaxResult.success(HttpClientUtil.sendGetRequest(requestUrl, null, null));
    }


    @GetMapping("/getPayPlatForm")
    public String getPayPlatForm(HttpServletRequest request, HttpServletResponse response){
        String userAgent = request.getHeader("user-agent");
        if (userAgent != null && userAgent.contains("MicroMessenger")){
            return "0";
        }else if (userAgent !=null && userAgent.contains("AlipayClient")){
            return "1";
        }
        return null;
    }

}
